package cgl.narada.transport;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * This class checks/keeps track of pending transfers within the registered
 * TransmissionReporters associated with the links.
 * 
 * @author Shrideep Pallickara
 * 
 */
public class TransmissionManager implements TransportDebugFlags {

    private Hashtable reporters;

    private String moduleName = "TransmissionManager: ";

    public TransmissionManager() {
        reporters = new Hashtable();
    }

    /**
     * Register a transmission reporter for a specific link ID
     * 
     * @param linkId
     *            The linkId of the link being tracked
     * @param reporter
     *            The reporter associated with the link
     */
    public synchronized void registerTransmissionReporter(String linkId,
                                             TransmissionReporter reporter) {
        if (linkId == null || reporter == null) {
            System.out.println(moduleName + "Problems registering reporter ("
                + reporter + ") for link =>" + linkId);
            return;
        }

        if (reporters.containsKey(linkId)) {
            if (TransmissionManager_Debug) {
                System.out.println(moduleName + "Reporter for link (" + linkId
                    + ") previously registered");
            }
            return;

        }

        if (TransmissionManager_Debug) {
            System.out.println(moduleName + "Registering link (" + linkId
                + ") with the Transmissions Manager");
        }
        reporters.put(linkId, reporter);
    }

    /**
     * Remove the transmission reporter associated with the link.
     * 
     * @param linkId
     */
    public synchronized void removeTransmissionReporter(String linkId) {
        if (linkId == null) {
            System.out.println(moduleName
                + "Problems removing reporter for link. "
                + "Specified linkId is NULL");
            return;
        }

        if (!reporters.containsKey(linkId)) {
            System.out.println(moduleName + "Unknown linkId => " + linkId
                + ") specified. Cannot remove corresponding reporter.");
            return;
        }

        reporters.remove(linkId);
    }

    public boolean hasPendingTransfers() {
        boolean pendingTransfer = false;
        if (reporters.size() == 0) {
            System.out.println(moduleName
                + "No reporters registered with transmission manager");
            pendingTransfer = false;
            return pendingTransfer;
        }

        Enumeration e = reporters.keys();

        while (e.hasMoreElements()) {
            Object linkId = e.nextElement();
            TransmissionReporter reporter = (TransmissionReporter) reporters
                .get(linkId);
            if (reporter == null) {
                continue;
            } else {
                if (reporter.hasPendingTransfer()) {
                    System.out.println(moduleName
                        + "There is a pending/ongoing transfer on " + linkId
                        + " for a total of "
                        + reporter.getSizeOfOngoingTransfer() + " bytes");
                    pendingTransfer = true;
                }
            }
        } /* end while */

        if (!pendingTransfer) {
            System.out
                .println(moduleName + "All transfers have been completed");
        }
        return pendingTransfer;
    }

    public synchronized void reset() {
        reporters.clear();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}
